package com.crm.controller;


import com.crm.mapper.UserMapper;
import com.crm.pojo.common.PageResult;
import com.crm.pojo.common.Result;
import com.crm.pojo.dto.UserDto;
import com.crm.pojo.vo.UserRoleVo;
import com.crm.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/system/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;
    //用户列表查询
    @RequestMapping("/list")
    public PageResult list(UserDto userDto,
                           @RequestParam(value = "params[beginTime]", required = false) String beginTime,
                           @RequestParam(value = "params[endTime]", required = false) String endTime) {
        userDto.setBeginTime(beginTime);
        userDto.setEndTime(endTime);
        log.info("{}",userDto);
        return userService.list(userDto);
    }
    //用户新增需要的获取角色
    @GetMapping
    public UserRoleVo userRoleVo (){
        return userService.userRoleVo();
    }
    //用户新增
    @PostMapping
    public Result add(@RequestBody UserDto userDto){
        try {
            userService.add(userDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }

    //查找某部门下有哪些员工
    @GetMapping("/listselect")
    public Result listSelect(Long deptId){
        return Result.success(userService.listSelect(deptId));
    }

    @DeleteMapping("/{userId}")
    public Result del(@PathVariable Integer userId){
        userService.del(userId);
        return Result.success();
    }

}
